package com.vader.admin.service.impl;

import com.vader.admin.dao.BaseUserInfoDao;
import com.vader.admin.entity.DO.BaseUserInfo;
import com.vader.admin.entity.Query.LoginQuery;
import com.vader.admin.service.SystemService;
import com.vader.common.entity.CommonResponse;
import com.vader.common.exception.BadRequestException;
import com.vader.common.utils.EncryptUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Vader
 * @date 2021/10/10
 **/
@Service
public class SystemServiceImpl implements SystemService {

    @Resource
    private BaseUserInfoDao baseUserInfoDao;

    @Override
    public CommonResponse<String> login(LoginQuery loginQuery) {
        BaseUserInfo baseUserInfo = baseUserInfoDao.queryByWechatId(loginQuery.getWechatId());
        if (Objects.isNull(baseUserInfo)) {
            throw new BadRequestException("请先进行注册");
        }
        if (baseUserInfo.getUserType() != 0) {
            throw new BadRequestException("没有登陆权限");
        }
        String password = baseUserInfo.getPassword();
        try {
            if (!loginQuery.getPassword().equals(EncryptUtils.desDecrypt(password))) {
                throw new BadRequestException("账号或密码错误");
            }
        } catch (Exception e) {
            throw new BadRequestException(e.getMessage());
        }
        return CommonResponse.success("登陆成功");
    }
}
